<?php

namespace app\teacher\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\learn\LearnSayVocabularyRecord as LearnSayVocabularyRecordModel;
use app\common\service\learn\LearnSayVocabularyService;

class LearnSayVocabularyRecord extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }


    //测试列表
    public function listVocabularyData()
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", 'studentId']);
        $user = $this->auth->getUserInfo();
        if (!empty($param['studentId'])) {
            $param['query']['EQ_studentId'] = $param['studentId'];
            $param['query']['EQ_tutorTrainerId'] = $user['id'];
        } else {
            $param['query']['EQ_trainerId'] = $user['id'];
        }
        $param['query']['EQ_isSubmit'] = 1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $LearnSayVocabularyRecordModel = new LearnSayVocabularyRecordModel();
        $list = $LearnSayVocabularyRecordModel->getVocabularyList($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('ok', result_row($list));

    }

    //开始测试
    public function startVocabulary()
    {
        $skRecordId = $this->request->post('skRecordId', null);
        $studentId = $this->request->post('studentId', null);
        $courseFullType = $this->request->post('courseFullType');
        if (!$courseFullType) {
            $this->fail('请选择测试类型');
        }
        $user = $this->auth->getUserInfo();
        $accountId = $user['id'];
        $accountType = AccountTypeEnum::TRAINER;
        $tutorTrainerId = null;
        if ($skRecordId) {
            if (!$studentId) {
                $this->fail('学生ID不可为空');
            }
            $accountType = AccountTypeEnum::STUDENT;
            $tutorTrainerId = $accountId;
            $accountId = $studentId;
        }

        $LearnSayVocabularyService = new LearnSayVocabularyService();
        $info = $LearnSayVocabularyService->startVocabulary($courseFullType, $accountId, $accountType, $skRecordId, $tutorTrainerId);
        $this->ok('ok', $info);
    }

    //提交测试
    // public function submitVocabulary()
    // {
    //     $params = $this->request->filter('trim')->post(['recordId', 'accountId', 'accountType', 'answerList']);
    //     $LearnSayVocabularyService = new LearnSayVocabularyService();
    //     $info = $LearnSayVocabularyService->submitVocabulary($params['accountId'], $params['accountType'], $params['answerList'], $params['recordId']);
    //     $this->ok('ok', $info);
    // }

    public function submitVocabulary()
    {
        $params = $this->request->filter('trim')->post(['recordId', 'accountId', 'accountType', 'answerList']);
        $LearnSayVocabularyService = new LearnSayVocabularyService();
        $info = $LearnSayVocabularyService->submitVocabulary($params['accountId'], $params['accountType'], $params['answerList'], $params['recordId']);
        $this->ok('ok', $info);
    }

    //获取报告
    public function getVocabularyRecord()
    {
        $vocabularyRecordId = $this->request->get('vocabularyRecordId');
        if (!$vocabularyRecordId) {
            $this->fail('请选择测试记录');
        }
        $LearnSayVocabularyService = new LearnSayVocabularyService();
        $info = $LearnSayVocabularyService->getVocabularyRecord($vocabularyRecordId);
        $this->ok('ok', $info);
    }
}